//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Drawing;
using DNAide.Drawing;

namespace DNAide.Web.UI.Controls
{

	/// <summary>
	/// Control for generating a resized copy of a source image
	/// </summary>
	public class ImageResize : ImageTransform
	{

		/// <summary></summary>
		public ImageResize()
		{
			ApplyTransformations += new ApplyTransformationsEventHandler(ImageResize_ApplyTransformations);
		}

		void ImageResize_ApplyTransformations(object sender, ref Bitmap bitmapToTransform)
		{
			if (!this.AllowUpScaling)
			{
				// Only if the current width or height exceeds the new dimensions do a resize.
				if (bitmapToTransform.Width > NewWidth || bitmapToTransform.Height > NewHeight)
				{
					BitmapAide.Resize(ref bitmapToTransform, NewWidth, NewHeight, MaintainAspectRatio, GraphicsSettings);
				}
			}
			else
			{
				BitmapAide.Resize(ref bitmapToTransform, NewWidth, NewHeight, MaintainAspectRatio, GraphicsSettings);
			}
		}
	

		/// <summary>
		/// 
		/// </summary>
		public Boolean AllowUpScaling
		{
			get { return ((ViewState["AllowUpScaling"] == null) ? true : (bool)ViewState["AllowUpScaling"]); }
			set { ViewState["AllowUpScaling"] = value; }
		}


		/// <summary>
		/// New width
		/// </summary>
		public Int32 NewWidth
		{
			get { return ((ViewState["NewWidth"] == null) ? 0 : (int)ViewState["NewWidth"]); }
			set { ViewState["NewWidth"] = value; }
		}

		/// <summary>
		/// New height
		/// </summary>
		public Int32 NewHeight
		{
			get { return ((ViewState["NewHeight"] == null) ? 0 : (int)ViewState["NewHeight"]); }
			set { ViewState["NewHeight"] = value; }
		}


		/// <summary>
		/// Whether to keep aspect ratio
		/// </summary>
		/// <remarks>
		/// Default = true
		/// </remarks>
		public Boolean MaintainAspectRatio
		{
			get { return ((ViewState["MaintainAspectRatio"] == null) ? true : (bool)ViewState["MaintainAspectRatio"]); }
			set { ViewState["MaintainAspectRatio"] = value; }
		}


		/// <summary>
		/// </summary>
		public GraphicsSettings GraphicsSettings
		{
			get { return ((ViewState["GraphicsSettings"] == null) ? null : (GraphicsSettings)ViewState["GraphicsSettings"]); }
			set { ViewState["GraphicsSettings"] = value; }
		}


		protected override void SetFinalWidthAndHeight()
		{
			base.SetFinalWidthAndHeight();

			if (string.IsNullOrEmpty(FinalPath) && !string.IsNullOrEmpty(FinalUrl))
			{
				// Handles scenario where image to resize is not found
				// and an alternative should be displayed in its place.
				FinalWidth = NewWidth;
				FinalHeight = NewHeight;
			}
		}


		/// <summary>
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderAttributes(System.Web.UI.HtmlTextWriter writer)
		{
			this.Attributes.Remove("AllowUpScaling");
			this.Attributes.Remove("GraphicsSettings");
			this.Attributes.Remove("MaintainAspectRatio");
			this.Attributes.Remove("NewHeight");
			this.Attributes.Remove("NewWidth");
			base.RenderAttributes(writer);
		}

		/// <summary>
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderEnd(System.Web.UI.HtmlTextWriter writer)
		{
			base.RenderEnd(writer);
		}

	}
}
